package com.xiaoq.manager.repository.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.ConstantUtil;
import com.xiaoq.coms.util.IdGeneratorUtils;
import com.xiaoq.manager.entity.MerchantMgrEntity;
import com.xiaoq.manager.repository.IMerchantMgrRepository;
import com.xiaoq.users.redis.RedisTemplate4JedisCluster;

@Repository
public class MerchantMgrRepositoryImpl implements IMerchantMgrRepository {

	@Autowired
	private Datastore datastore;
	
	@Autowired
	private RedisTemplate4JedisCluster redisTemplate;
	
	private static final Logger LOG = LoggerFactory.getLogger(MerchantMgrRepositoryImpl.class);
	
	@Override
	public MerchantMgrEntity save(MerchantMgrEntity merchant) {
		//不完全
		datastore.save(merchant);
		String managerId = merchant.getId();
		String shopId = merchant.getShopId();
		if(StringUtils.isNotEmpty(managerId)&&StringUtils.isNotEmpty(shopId)){
			redisTemplate.set(ConstantUtil.REDIS_PREFIX_MANAGER_SHOP_RELATION, managerId, shopId);
		}
		return merchant;
	}

	@Override
	public MerchantMgrEntity findByMobile(String mobile) {
		Query<MerchantMgrEntity> merchantMgrQuery = datastore.find(MerchantMgrEntity.class);
		merchantMgrQuery.field("mobile").equal(mobile);
		if(merchantMgrQuery==null||merchantMgrQuery.count()==0){
			return null;
		}
		
		MerchantMgrEntity merchantMgr = merchantMgrQuery.get();
		LOG.info(merchantMgr.getStatus()+"");
		if(merchantMgr.getStatus()==1){
			return null;
		}else{
			return merchantMgr;
			
		}
	}

	@Override
	public MerchantMgrEntity findById(String id) {
		return datastore.get(MerchantMgrEntity.class,id);
	}

	@Override
	public List<MerchantMgrEntity> findByShopId(String shopId) {
		Query<MerchantMgrEntity> merchantQuery = datastore.find(MerchantMgrEntity.class);
		merchantQuery.field("shopId").equal(shopId);
		return merchantQuery.asList();
	}

	@Override
	public boolean delMerchantMgr(String id) {
		Query<MerchantMgrEntity> merchantQuery = datastore.find(MerchantMgrEntity.class);
		merchantQuery.field(IdGeneratorUtils.IDGENERATOR).equal(id);
		datastore.delete(merchantQuery);
		return true;
	}

	@Override
	public boolean updateMerchantMgr(MerchantMgrEntity merchantMgr) {
		datastore.save(merchantMgr);
		return true;
	}

	@Override
	public boolean updateMerchantMgrStatus(String id, int status) {
		MerchantMgrEntity merchantMgr = datastore.get(MerchantMgrEntity.class,id);
		merchantMgr.setStatus(status);
		datastore.save(merchantMgr);
		return true;
	}

	@Override
	public MerchantMgrEntity findByEmail(String email) {
		MerchantMgrEntity merchant = datastore.find(MerchantMgrEntity.class).field("email").equal(email).get();
		return merchant;
	}

	@Override
	public List<MerchantMgrEntity> findByRoleId(String roleId,String shopId) {
		Query<MerchantMgrEntity> merchantQuery = datastore.find(MerchantMgrEntity.class);
		merchantQuery.field("roleId").equal(roleId);
		merchantQuery.field("shopId").equal(shopId);
		return merchantQuery.asList();
	}

	@Override
	public MerchantMgrEntity findByIdByMerchantId(String merchantId) {
		MerchantMgrEntity merchant = datastore.find(MerchantMgrEntity.class).field("merchantId").equal(merchantId).get();
		return merchant;
	}

}
